package com.zxf.orderservice.controller;

import com.zxf.orderservice.service.OrderService;
import com.zxf.orderservice.service.OrderServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    RestTemplate restTemplate;
    @Autowired
    OrderService orderService;

    @GetMapping("/stock/deduct")
    public String deductStock(@RequestParam("productId") Long productId, @RequestParam("stockCount") Long stockCount){
        return this.restTemplate.getForObject("http://create-service/stock/deduct/"+ productId + "/" + stockCount,
                String.class);
    }
    @GetMapping("/create")
    public String createOrder(
            @RequestParam("productId") Long productId,
            @RequestParam("userId") Long userId,
            @RequestParam("stockCount") Integer stockCount,
            @RequestParam("creditCount") Integer creditCount){
        orderService.createOrder(productId,userId,stockCount,creditCount);
        return "success";
    }
}
